package com.longdaji.decoration.ui.activitiesOfMine.refund;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.RefundAdapter;
import com.longdaji.decoration.adapter.RefundOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.bean.RefundGoods;
import com.longdaji.decoration.ui.activitiesOfMine.refund.forRefund.ForRefundActivity;
import com.longdaji.decoration.ui.activitiesOfMine.refund.refundDetail.RefundDetailActivity;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundActivity extends BaseActivity implements RefundContract.View {

    private static final String TAG = "RefundActivity";

    @Inject
    RefundContract.Presenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_refund)
    RecyclerView rvRefund;
    @BindView(R.id.sr_refund)
    SwipeRefreshLayout srRefund;

    //    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();
    //    private RefundOrderAdapter mAdapter;
    private List<RefundGoods> mRefundGoodsList = new ArrayList<>();
    private RefundAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("退款/售后");

        initView();

    }

    private void initView() {
        rvRefund.setLayoutManager(new LinearLayoutManager(this));
//        mAdapter = new RefundOrderAdapter(mOrderList);
//        mAdapter.setSeeDetailListener(new RefundOrderAdapter.SeeDetailListener() {
//            @Override
//            public void onSeeDetailClick(int position) {
//                Intent intent = new Intent(RefundActivity.this, RefundDetailActivity.class);
//                intent.putExtra("orderNo", mOrderList.get(position).getOrderNo());
//                startActivity(intent);
//            }
//        });
        mAdapter = new RefundAdapter(mRefundGoodsList);
        mAdapter.setRefundGoodsListener(new RefundAdapter.RefundGoodsListener() {
            @Override
            public void onSeeDetailsClick(int position) {
                Intent intent = new Intent(RefundActivity.this, RefundDetailActivity.class);
                intent.putExtra(Constants.ORDER_NO, mRefundGoodsList.get(position).getOrderNo());
                intent.putExtra(Constants.GOODS_ID, mRefundGoodsList.get(position).getGoodsId());
                startActivity(intent);
            }
        });
        rvRefund.setAdapter(mAdapter);
        srRefund.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srRefund.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();
    }

    private void refreshData() {
        mPresenter.initData();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

//    @Override
//    public void showRefundOrders(List<Orders.OrdersBean.OrderBean> refundOrderList) {
//        if (refundOrderList.size() > 0) {
//            mAdapter.setData(refundOrderList);
//        } else {
//            Toast.makeText(this, "无退款订单", Toast.LENGTH_SHORT).show();
//            mAdapter.clearData();
//        }
//    }

    @Override
    public void showRefundGoods(List<RefundGoods> refundGoodsList) {
        LogUtil.i(TAG, "showRefundGoods: " + refundGoodsList.size());
        Collections.reverse(refundGoodsList);
        mAdapter.setData(refundGoodsList);
        if (refundGoodsList.size() > 0) {
            mAdapter.setData(refundGoodsList);
        } else {
            Toast.makeText(this, "无退款商品", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }

        if (srRefund != null && srRefund.isRefreshing()) {
            srRefund.setRefreshing(false);
        }
    }
}
